'use strict'

const { TaiSpider } = require('tai-spider');

class MissileCsisSpider extends TaiSpider {

    constructor(options = {}) {
        super(options);
        this.name = 'missile-csis';
        this.debug = true;
        this.start_urls = [
            'https://missilethreat.csis.org/missile/'
        ];
    }

    *parse(response) {
        for (let ele of response.css('#item-select option.component-select__option')) {
            yield response.follow(ele.attr('value'), this.parseArticle);
        }
    }

    *parseArticle(response, spider) {
        let glanceEle = response.css('div.at-a-glance').get(0);
        let glance = this.parseGlance(glanceEle);
        yield Object.assign({
            index_name: 'tai-missile',
            missile_name: response.css('h1.single__header-title').extract_first(),
            last_update: response.css('div.single__header-meta div.post-meta__date').extract_first(),
            ...glance,
        }, response.extract());
    }

    parseGlance(ele) {
        let record = {}
        for (let dl of ele.css('dl')) {
            record[this.formatFieldName(dl.css('dt').extract_first())] = dl.css('dd').extract_first()
        }
        return record;
    }

    formatFieldName(str) {
        return str.toLowerCase().replace(/ /g, '_');
    }
}

module.exports = MissileCsisSpider;